<?php
/**
 * 园所管理
 * @author a
 *
 */
class Nursery extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('nursery_model');
        $this->load->model('area_model');
        $this->load->model('city_model');
        
        //分页
//         $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
//         $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
//         $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    public function Index(){
        try {
            $where['id'] = $this->_nursery_id;
            $where['status'] = 1;
            $nursery_info = $this->nursery_model->find($where);
            if(empty($nursery_info))throw new Exception('没有记录');
            $this->t->write_view('content', 'nursery/nursery/info', array(
            		'nursery_info'	=>	$nursery_info,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'nursery/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                //'url'   =>  '/nursery/nursery/',
            ), false);
            $this->t->render();exit();
        }
    }
    /**
     * 修改
     */ 
    public function Update(){
        try {
            $id = intval($_REQUEST['id']);
            if(empty($id))throw new Exception('ID为空');
            $form_submit = $this->input->post('form_submit');
            if(empty($form_submit))throw new Exception('修改失败');
            
            $data = array();
            $data['area_id'] = intval($this->input->post('area_id'));
            $data['city_id'] = intval($this->input->post('city_id'));
            $data['title'] = $this->input->post('title');
            $data['address'] = $this->input->post('address');
            $data['explain'] = $this->input->post('explain');
            $data['telephone'] = $this->input->post('telephone');
            $data['update_date'] = date('Y-m-d H:i:s');
            $bool = $this->nursery_model->update($data,array('id'=>$id));
            if(empty($bool))throw new Exception('修改失败');
            redirect("/nursery/nursery");
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'nursery/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/nursery/nursery/',
            ), false);
            $this->t->render();
        }
    }
    /**
     * 获取地区
     */
    public function Getarea(){
    	try{
    		$return = array('code'=>10,'msg'=>'成功','list'=>array());
    		$list = $this->area_model->findAll(array('status'=>1),'id,title');
    		if(empty($list))throw new Exception('不存在');
    		$return['list'] = $list;
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 获取市区
     */
    public function Getcity(){
    	try{
    		$return = array('code'=>10,'msg'=>'成功','list'=>array());
    		$area_id = intval($this->input->get('area_id'));
    		if(empty($area_id))throw new Exception('地区ID为空');
    		$list = $this->city_model->findAll(array('status'=>1,'area_id'=>$area_id),'id,title');
    		if(empty($list))throw new Exception('不存在');
    		$return['list'] = $list;
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    
    
    
    
    
    
    
    
}






























